package com.news.web.servlet.admin;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.News;
import com.news.service.NewsService;
import com.news.service.impl.NewsServiceImpl;

/**
 * Servlet implementation class AdminNewsUpdateServlet
 */
@WebServlet("/AdminNewsUpdateServlet")
public class AdminNewsUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 获取传入数据
		String newsIdStr = request.getParameter("newsId");
		String newsType = request.getParameter("newsType");
		String newsHeadline = request.getParameter("newsHeadline");
		String newsIntro = request.getParameter("newsIntro");
		String newsDetails = request.getParameter("newsDetails");
		String newsAuthor = request.getParameter("newsAuthor");
		String newsDateStr = request.getParameter("newsDate");
		String newsImg = request.getParameter("newsImg");
		String newsHeatStr = request.getParameter("newsHeat");
		String newsReviewNumStr = request.getParameter("newsReviewNum");
		// 判断日期参数是否有值，若没有值，返回原页面
		if (newsDateStr == null || "".equals(newsDateStr)) {
			request.setAttribute("newsInsertBoolean", null); // 或者设定为null
			request.getRequestDispatcher("/admin/adminNewsInsertAndUpdate.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}
		// 定义日期格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		// 进行数据格式转换
		int newsId = 0;
		int newsHeat = 0;
		Timestamp newsDate = null;
		int newsReviewNum = 0;
		try {
			// 将字符串解析为日期
			java.util.Date parsedDate = dateFormat.parse(newsDateStr);
			newsId = Integer.parseInt(newsIdStr);
			newsHeat = Integer.parseInt(newsHeatStr);
			newsDate = new Timestamp(parsedDate.getTime());
			newsReviewNum = Integer.parseInt(newsReviewNumStr);
		} catch (NumberFormatException | ParseException e) {
			// 处理数值转换异常的情况
			e.printStackTrace(); // 记录日志或者向用户显示错误信息
			request.setAttribute("newsUpdateBoolean", null); // 或者设定为null
			request.getRequestDispatcher("/admin/adminNewsInsertAndUpdate.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}
		// 实例化News
		News news = new News(newsId, newsType, newsHeadline, newsIntro, newsDetails, newsAuthor, newsDate, newsImg,
				newsHeat, newsReviewNum);
		// 创建NewsService实例
		NewsService newsService = new NewsServiceImpl();
		// 使用修改方法
		Boolean newsUpdateBoolean = newsService.updateNews(news);
		// 使用模糊查询，设置时间为null
		news.setNewsDate(null);
		List<News> newsQueryLikeList = newsService.likeSelectListReview(news);
		// 将数据存入
		request.setAttribute("newsQueryLikeList", newsQueryLikeList);
		// 将数据存入
		request.setAttribute("newsUpdateBoolean", newsUpdateBoolean);
		// 跳转回原页面
		request.getRequestDispatcher("/admin/adminNewsQueryAndDelete.jsp").forward(request, response);
	}

}
